﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;

namespace BitPortal.Modules.NewsReference
{
    public partial class NewsReference : Controls.BaseUserControl
    {
        public bool EnableToolTips =  true;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ltrTitle.Text = ReferenceModule.GetModuleConfig("Header").ToString();
            bool? IsHot = Convert.ToBoolean(ReferenceModule.GetModuleConfig("IsHot")) ? (bool?)true : null;
            bool Newest = Convert.ToBoolean(ReferenceModule.GetModuleConfig("Newest"));
            bool Before = Convert.ToBoolean(ReferenceModule.GetModuleConfig("Before"));
            bool After = Convert.ToBoolean(ReferenceModule.GetModuleConfig("After"));
            EnableToolTips = Convert.ToBoolean(ReferenceModule.GetModuleConfig("EnableToolTip"));
            int NumberOfArticle = ReferenceModule.GetModuleConfig("NumberOfArticles").ToInt32TryParse();
            if (NumberOfArticle <= 0) NumberOfArticle = 5;
            Article currentArticle = ArticleBLL.GetByID(Request.QueryString["ArticleID"].ToInt64TryParse());
            if (currentArticle == null)
            {
                this.Visible = false;
                return;
            }
            IQueryable<Article> listA = ArticleBLL.GetByCondition<Int64>(categoryID:currentArticle.CategoryID,isApproved:true,isEnabled:true,isHot:IsHot);
            if (After)
            {
                listA = listA.Where(ent => ent.ArticleID > currentArticle.ArticleID);
            }
            else if (Before)
            {
                listA =  listA.Where(ent => ent.ArticleID < currentArticle.ArticleID);
            }
            if (listA.Count() == 0)
            {
                this.Visible = false;
                return;
            }
            listA = listA.Take(NumberOfArticle);
            this.RptArticles.DataSource = listA;
            this.RptArticles.DataBind();
        }
    }
}